package com.jk.mapper;

import com.jk.util.TutorDynaSqlProvider;
import com.jk.vo.CouponUser;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

public interface CouponOfUserMapper {



    int insert(CouponUser record);

    /**
     * 添加用户 优惠劵
     * @param couponUser
     * @return
     */
    @InsertProvider(type=TutorDynaSqlProvider.class,
            method="insertSelective")
    int insertSelective(CouponUser couponUser);

    CouponUser selectByPrimaryKey(String couponId);

    /**
     *定时修改用户优惠劵状态
     * @param date
     * @return
     */
    @Update("UPDATE coupon_user SET coupon_user.`coupon_Status` = 3 " +
            " WHERE coupon_user.`coupon_Status` = 1 " +
            " AND coupon_user.`coupon_id` IN ( SELECT a.coupon_id FROM " +
            " (SELECT coupon_id FROM coupon_info" +
            " WHERE coupon_over_time < #{data}) a) ")
    int updateByPrimaryKeySelective(Date date);

    /**
     * 优惠劵领取OK数量减一
     * @param couponId
     * @return
     */
    @UpdateProvider(type=TutorDynaSqlProvider.class,
            method="updateByPrimaryKey")
    int updateByPrimaryKey(String couponId);

    /**
     * 定时删除过期七天的用户优惠劵
     * @param pastDate
     * @return
     */
    @Delete("DELETE FROM coupon_user WHERE coupon_id IN ( SELECT a.coupon_id FROM " +
            "(SELECT coupon_id FROM coupon_info " +
            " WHERE coupon_over_time < #{pastDate})a)")
    int deleteByPrimaryKey(Date pastDate);
    /**
     * 定时删除过期七天的优惠劵
     * @param pastDate
     * @return
     */
    @Delete("DELETE FROM coupon_info WHERE coupon_over_time < #{pastDate}")
    int deleteByCouponInOfKey(Date pastDate);

    /**
     * 定时修改优惠卷过期状态
     * @param date
     * @return
     */
    @Update("UPDATE coupon_info SET coupon_status = 3 " +
            "WHERE coupon_over_time <#{date}")
    int updateByCouponInOfKeySelective(Date date);

    /**
     * 查看用户是否领过次优惠劵
     * @param couponUser
     * @return
     */
    @Select("SELECT COUNT(1) FROM coupon_user " +
            "WHERE coupon_user.`coupon_id` = #{couponId} " +
            "AND coupon_user.`user_id` = #{userId}")
    int queryCouponUser(CouponUser couponUser);

    /**
    *优惠劵使用完后修改状态
     *条件：优惠劵id ，用户id
    */
    @Update("UPDATE coupon_user SET coupon_Status = 2 " +
            "WHERE coupon_id = #{couponId} and user_id = #{userId}")
    int updateUserCouponStatus(CouponUser couponUser);
}
